package com.easyCode.mysql.sqlBuilder.sqlInfo;

import com.easyCode.mysql.constants.MysqlKeyTypeConstants;
import com.easyCode.sqlBuilder.sqlInfo.BaseKeySqlInfo;
import com.easyCode.sqlBuilder.sqlUtil.SqlBuildUtil;
import com.easyCode.util.StringUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;


/**
 * 提供构建索引sql相关信息 <br>
 * 索引创建的通用规则:
 * PRIMARY KEY/UNIQUE KEY/KEY 索引名 (字段名1,字段名2,...)
 *
 * @author 大忽悠
 * @create 2022/11/30 15:25
 */
@Data
public class MysqlKeySqlInfo extends BaseKeySqlInfo {

    @Override
    public boolean isPrimary() {
        return keyType.equals(MysqlKeyTypeConstants.PRIMARY_KEY);
    }

    /**
     * @return 根据自身掌握的sql信息构建出一部分或者完整的sql语句
     */
    @Override
    public String doBuildSql() {
        //检查外键
        if (keyType.equalsIgnoreCase(MysqlKeyTypeConstants.FOREIGN_KEY)) {
            return SqlBuildUtil.buildSql(keyType, keyName, "(", String.join(",", salveColumns), ")", "REFERENCES", masterTableName, "(", String.join(",", masterColumns), ")");
        }
        return SqlBuildUtil.buildSql(keyType, keyName, "(", String.join(",",columns), ")");
    }
}
